//
//  AlertLoginOutView.m
//  i5db
//
//  Created by iMuse on 15-5-19.
//  Copyright (c) 2015年 mySelf. All rights reserved.
//

#import "AlertLoginOutView.h"
#import "AppDelegate.h"
#import "CooperateController.h"

@implementation AlertLoginOutView
{
    UIView * backgroundView;
    UIImageView * imageView ;
    
}

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        
        imageView = [[UIImageView alloc] init];
        imageView.frame = frame;
        imageView.backgroundColor = [UIColor blackColor];
        imageView.alpha     =0;
        imageView.center = self.center;
        [self addSubview:imageView];
        
        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackground:)];
        [self addGestureRecognizer:tapGesture];
        
        backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 90)];
        backgroundView.layer.cornerRadius = 4;
        backgroundView.center = self.center;
        backgroundView.alpha = 0;
        backgroundView.backgroundColor = [UIColor whiteColor];
        [self addSubview:backgroundView];
        
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 160, 30)];
        label.font = [UIFont systemFontOfSize:10];
        label.numberOfLines = 0;
        label.text = @"退出登录不会删除任何历史记录,下次登录依然可以使用本账号。";
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor blackColor];
        [backgroundView addSubview:label];
        
        UIButton * outButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [outButton addTarget:self action:@selector(outCilck:) forControlEvents:UIControlEventTouchUpInside];
        outButton.frame = CGRectMake(10, 45, 140, 35);
        outButton.layer.cornerRadius = 4;
        outButton.backgroundColor = [UIColor redColor];
        [outButton setTitle:@"退出登录" forState:UIControlStateNormal];
        [outButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [backgroundView addSubview:outButton];
    }
    
    return self;
}
-(void)startAnimation
{
    [UIView animateWithDuration:0.2 animations:^{
        imageView.alpha = 0.4;
        backgroundView.alpha = 1;
        
    }completion:^(BOOL finash){
        
    }];
}
-(void) outCilck:(UIButton*) button
{
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [appDelegate showLogin];
 
    [self.controller.navigationController popToRootViewControllerAnimated:YES];
    [self tapBackground:nil];
   
}

-(void)tapBackground:(UITapGestureRecognizer*)gester
{
    [UIView animateWithDuration:0.2 animations:^{
        
        imageView.alpha = 0;
        backgroundView.alpha = 0;
        
    }completion:^(BOOL finash){
        
        [self removeFromSuperview];
        
    }];
    
}


+(UIViewController*)rootController
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }
    return topController;
}

+(void) alertLoginOut:(UIViewController*) controller
{
    UIViewController * rootController = [self rootController];
    AlertLoginOutView * alertView = [[AlertLoginOutView alloc] initWithFrame:rootController.view.bounds];
    [rootController.view addSubview:alertView];
    alertView.controller = controller;
    [alertView startAnimation];
    
}


@end
